#include "app_protocol.h"

#define RECVTIMEOUT 1000 // ms

void package_message(uint8_t *data, int len) // zi dingyi xieyi
{
	uint8_t message[1500] = "";
	uint8_t checksum = 0;
	// uint16_t length = len + 1;
	message[0] = 0x55;
	message[1] = 0xAA;
	message[2] = 0x81;
	message[3] = 0x30;
	// printf("test message!\n");
	//seq
	message[4] = 0x00;
	message[5] = 0x00;
//数据长度
	message[6] = 0x00;
	message[7] = 0x00;
	message[8] = (uint8_t)(len>>8);
	message[9] = (uint8_t)(len & 0xFF);
	// printf("4==%x,5==%x\n",message[4],message[5]);
	memcpy(message + 10, data, len); // all 7+len
	for (int i = 0; i < 10 + len; i++)
	{
		checksum += message[i];
		// printf("message[%d]==%02x\n",i,message[i]);
	}
	// printf("checksum==%02x\n",checksum);
	message[10 + len] = checksum;

	message[11 + len] = 0xFE;
	
	uart_handle.send_data(message, 12 + len);
}

void send_mcu_version(uint8_t version1, uint8_t version2, uint8_t version3)
{
	uint8_t mcu_version[15] = "";
	mcu_version[0] = 0x55;
	mcu_version[1] = 0xAA;
	mcu_version[2] = 0x01;
	mcu_version[3] = 0x20;
	mcu_version[4] = 0x00;
	mcu_version[5] = 0x00;
	mcu_version[6] = 0x00;
	mcu_version[7] = 0x00;
	mcu_version[8] = 0x00;
	mcu_version[9] = 0x03;
	mcu_version[10] = version1;
	mcu_version[11] = version2;
	mcu_version[12] = version3;
	// xQueueReset(uart_handle.uartqueue);

	for (int i = 0; i < 13; i++)
	{
		mcu_version[13] += mcu_version[i];
	}
	mcu_version[14] = 0xFE;
	uart_handle.send_data(mcu_version, sizeof(mcu_version));

	// uint8_t wifiuart[] = {0x55, 0xaa, 0x01, 0x0B, 0x00, 0x00, 0x0B, 0xfe};
	// uart_handle.send_data(wifiuart,  sizeof(wifiuart));
	// uint8_t uartrecv[100] = "";

	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
}

void start_net_config(void)
{
	uint8_t config_net_cmd[] = {0x55, 0xaa, 0x01, 0x04, 0x00, 0x00, 0x04, 0xfe};
	// xQueueReset(uart_handle.uartqueue);
	uart_handle.send_data(config_net_cmd, sizeof(config_net_cmd));
	// uint8_t uartrecv[100] = "";
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
}

// int get_net_time(float tim)
// {
// 	uint8_t uartrecv[100] = "";
// 	// xQueueReset(uart_handle.uartqueue);
// 	uint8_t timestring[16] = "";
// 	uint8_t time_zone[] = {16, 17, 18, 19, 20, 21, 22, 23, 00, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
// 	int timestamp = 0;
// 	// 55 AA 01 09 00 00 09 FE
// 	uint8_t gettimecmd[] = {0x55, 0xAA, 0x01, 0x09, 0x00, 0x00, 0x09, 0xFE};
// 	xQueueReset(uart_handle.uartqueue);
// 	uart_handle.send_data(gettimecmd, sizeof(gettimecmd));

// 	if (xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT))
// 	{
// 		USART_SendData_DMA(uartrecv, sizeof(uartrecv));
// 		if ((uartrecv[0] == 0x55) && (uartrecv[1] == 0xAA) && (uartrecv[2] == 0x00) &&
// 			(uartrecv[3] == 0x09) && (uartrecv[4] == 0x00) && (uartrecv[5] == 0x08) && (uartrecv[16] == 0))
// 			;
// 		else
// 			return tim;
// 	}

// 	memcpy(timestring, uartrecv, 16);

// 	struct tm info;
// 	info.tm_year = timestring[6] * 100 + timestring[7] - 1900;
// 	info.tm_mon = timestring[8] - 1; //
// 	info.tm_mday = timestring[9];
// 	info.tm_hour = time_zone[timestring[11]]; // timestring[9] mon thu ...
// 	info.tm_min = timestring[12];
// 	info.tm_sec = timestring[13];
// 	timestamp = mktime(&info);
// 	return timestamp;
// }


// void package_realsosdata(int breathrate, int strokeRisk, int inBedtime, int stateInBbed, int *bodyMovementData,int sosflag)
// {
//     uint8_t uartrecv[100] = "";

//     uint32_t dataint[28];  // 从28改为29，以容纳新增的sosflag和24个bodyMovementData
//     dataint[0] = breathrate;      // 呼吸率
//     dataint[1] = strokeRisk;      // 中风风险
//     dataint[2] = inBedtime;       // 在床时间
//     dataint[3] = stateInBbed;     // 在床状态
//     dataint[4] = sosflag;         // 新增的SOS标志位

//     // 将bodyMovementData存入dataint[5]~dataint[28]（共24个int）
//     for (int i = 5; i < 29; i++)  // 注意：i < 29
//     {
//         dataint[i] = bodyMovementData[i - 5];
//     }

//     uint8_t datachar[116];        // 原112改为116（29*4=116字节）
//     memcpy(dataint + 5, bodyMovementData, 24 * sizeof(int)); // 拷贝24个int
//     memcpy(datachar, dataint, sizeof(datachar));

//     uint8_t datacharsend[120];    // 原116改为120（116+4字节头部）
//     int i = 0;
//     memcpy(datacharsend, &i, sizeof(int));
//     memcpy(datacharsend + 4, datachar, sizeof(datachar));

//     xQueueReset(uart_handle.uartqueue);
//     protocol_handle.package_message(datacharsend, sizeof(datacharsend));
//     xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
//     xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
// }

extern uint8_t wifi_mac[6];
extern uint8_t IMEI_arr[6];
extern ExtY_nczallcode_T nczallcode_Ytemp;
void package_realsosdata(int breathrate, int strokeRisk, int inBedtime, int stateInBbed, int *bodyMovementData,uint8_t sosflag,uint8_t current_threshold,uint8_t on_bed_duration_minutes, uint8_t sitting_alarm_duration_minutes,uint8_t falling_alarm_duration_minutes,uint8_t merged_alarm)
{

	uint8_t uartrecv[1] = "";

	uint32_t dataint[28];
	dataint[0] = breathrate;
	dataint[1] = strokeRisk;
	dataint[2] = inBedtime;
	dataint[3] = stateInBbed; // dataint[4] = bodyMovementData;
	for (int i = 4; i < 28; i++)
	{
		dataint[i] = bodyMovementData[i - 4];
	}
	uint8_t datachar[112];
	memcpy(dataint + 4, bodyMovementData, 24 * sizeof(int));
	memcpy(datachar, dataint, sizeof(datachar));

	uint8_t datacharsend[134];//123+3回传flash数据//134
	int i = 0;
	memcpy(datacharsend, &i, sizeof(int));
	memcpy(datacharsend + 4, datachar, sizeof(datachar));

	datacharsend[116] = sosflag;

	memcpy(datacharsend + 117, IMEI_arr, sizeof(IMEI_arr));
	datacharsend[123] = current_threshold ;
	datacharsend[124] = on_bed_duration_minutes;
	datacharsend[125] = sitting_alarm_duration_minutes;
	datacharsend[126] = falling_alarm_duration_minutes;
	datacharsend[127] = merged_alarm;
	datacharsend[128] = (uint8_t)stateInBbed;
	datacharsend[129] = (uint8_t)nczallcode_Ytemp.alarm_counter[0];
	datacharsend[130] = (uint8_t)nczallcode_Ytemp.alarm_counter[1];
	datacharsend[131] = (uint8_t)nczallcode_Ytemp.alarm_counter[2];
	datacharsend[132] = (uint8_t)nczallcode_Ytemp.alarm_counter[3];
	datacharsend[133] = (uint8_t)nczallcode_Ytemp.runtime;
	// xQueueReset(uart_handle.uartqueue);
	protocol_handle.package_message(datacharsend, sizeof(datacharsend));
	xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	//测试打印回传数据
    // USART_SendData_DMA(m_au8RxBuf, m_u16RxLen);
}

void package_A0message(uint8_t current_threshold,uint8_t on_bed_duration_minutes, uint8_t sitting_alarm_duration_minutes,uint8_t falling_alarm_duration_minutes,uint8_t merged_alarm)
{
	uint8_t uartrecv[100] = "";
	uint8_t A0message[16];

	memcpy(A0message, wifi_mac, sizeof(wifi_mac));
	A0message[6] = current_threshold ;
	A0message[7] = on_bed_duration_minutes;
	A0message[8] = sitting_alarm_duration_minutes;
	A0message[9] = falling_alarm_duration_minutes;
	A0message[10] = merged_alarm;
	A0message[11] = (uint8_t)nczallcode_Y.alarm_counter[0];
	A0message[12] = (uint8_t)nczallcode_Y.alarm_counter[1];
	A0message[13] = (uint8_t)nczallcode_Y.alarm_counter[2];
	A0message[14] = (uint8_t)nczallcode_Y.alarm_counter[3];
	A0message[15] = (uint8_t)nczallcode_Y.runtime;
	xQueueReset(uart_handle.uartqueue);
	protocol_handle.package_message(A0message, sizeof(A0message));
	xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	//测试打印回传数据
    // USART_SendData_DMA(m_au8RxBuf, m_u16RxLen);
}


// void package_realtimedata(int breathrate, int strokeRisk, int inBedtime, int stateInBbed, int *bodyMovementData)
// {

// 	uint8_t uartrecv[100] = "";

// 	uint32_t dataint[28];
// 	dataint[0] = breathrate;
// 	dataint[1] = strokeRisk;
// 	dataint[2] = inBedtime;
// 	dataint[3] = stateInBbed; // dataint[4] = bodyMovementData;
// 	for (int i = 4; i < 28; i++)
// 	{
// 		dataint[i] = bodyMovementData[i - 4];
// 	}
// 	uint8_t datachar[112];
// 	memcpy(dataint + 4, bodyMovementData, 24 * sizeof(int));
// 	memcpy(datachar, dataint, sizeof(datachar));

// 	uint8_t datacharsend[116];
// 	int i = 0;
// 	memcpy(datacharsend, &i, sizeof(int));
// 	memcpy(datacharsend + 4, datachar, sizeof(datachar));

// 	xQueueReset(uart_handle.uartqueue);
// 	protocol_handle.package_message(datacharsend, sizeof(datacharsend));
// 	xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
// 	xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
// }

void package_oneminsdata(int ratemin, int strokeriskmin, int *statesleep, int timestamp, int mean, unsigned char *martix, uint8_t bedType)
{
	uint8_t uartrecv[1] = "";
	uint32_t dataint[52];

	dataint[0] = bedType;

	dataint[1] = strokeriskmin;
	memcpy(dataint + 2, statesleep, 48 * sizeof(int));
	dataint[50] = timestamp;
	dataint[51] = mean;
	uint8_t datachar[208];
	memcpy(datachar, dataint, sizeof(datachar));

	// uint8_t datacharsend[212];//+1024
	// uint8_t datacharsend[1237];
	// uint8_t datacharsend[1241];
	// int i = 1;
	// memcpy(datacharsend, &i, sizeof(int));
	// memcpy(datacharsend + 4, datachar, sizeof(datachar));

	// memcpy(datacharsend + 212, martix, 1024);
	// // uint32_t testbreath=100;
	// datacharsend[1240] = (uint8_t)ratemin;
	// xQueueReset(uart_handle.uartqueue);
	// protocol_handle.package_message(datacharsend, sizeof(datacharsend));
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);

	// uint8_t datacharsend[212];//+1024
	uint8_t datacharsend[1237];
	int i = 1;
	memcpy(datacharsend, &i, sizeof(int));
	memcpy(datacharsend + 4, datachar, sizeof(datachar));

	memcpy(datacharsend + 212, martix, 1024);
	// uint32_t testbreath=100;
	datacharsend[1236] = (uint8_t)ratemin;
	// xQueueReset(uart_handle.uartqueue);
	// USART_SendData_DMA(datacharsend, sizeof(datacharsend));
	protocol_handle.package_message(datacharsend, sizeof(datacharsend));
	
	xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
}

/*
0x00：WIFI 处于配网状态
0x01：WIFI 未连接到路由器，未连接到服务器
0x02：WIFI 连接到路由器，未连接到服务器
0x03：WIFI 连接到路由器，并连接到服务器，未鉴权成功
0x04：WIFI 连接到路由器，并连接到服务器，鉴权成功
*/
// int get_net_status(void)
// {
// 	// 55 AA 01 05 00 00 05 FE   mcu>>wifi
// 	// 55 AA 00 05 00 01 04 09 FE  wifi>>mcu
// 	uint8_t getnetflag = 1;
// 	uint8_t getnetstatus_cmd[] = {0x55, 0xAA, 0x01, 0x05, 0x00, 0x00, 0x05, 0xFE};
// 	xQueueReset(uart_handle.uartqueue);
// 	uart_handle.send_data(getnetstatus_cmd, sizeof(getnetstatus_cmd));
// 	uint8_t uartrecv[100] = "";
// 	if (xQueueReceive(uart_handle.uartqueue, uartrecv, 3000))
// 	{
// 		if ((uartrecv[0] == 0x55) && (uartrecv[1] == 0xAA) && (uartrecv[2] == 0x00) && (uartrecv[3] == 0x05) && (uartrecv[4] == 0x00) && (uartrecv[5] == 0x01) && (uartrecv[8] == 0xFE))
// 			getnetflag = 1;
// 		else
// 			getnetflag = 0;
// 	}
// 	else
// 	{
// 		return 999;
// 	}
// 	if (getnetflag == 1)
// 		return uartrecv[6];
// 	else
// 		return 999;
// }

//灯控LED状态
uint8_t net_led = 0x04;

//获取LED灯状态
uint8_t get_net_led(void)
{
    return net_led;
}

//设置LED灯状态
void set_net_led(uint8_t led_status)
{
	net_led = led_status;
}

void get_net_status(void)
{
    // 55 AA 01 05 00 00 05 FE   mcu>>wifi
    // 55 AA 00 05 00 01 04 09 FE  wifi>>mcu
    uint8_t getnetstatus_cmd[] = {0x55, 0xAA, 0x01, 0x05, 0x00, 0x00, 0x05, 0xFE};
    // xQueueReset(uart_handle.uartqueue);
    uart_handle.send_data(getnetstatus_cmd, sizeof(getnetstatus_cmd));

    // uint8_t uartrecv[30] = ""; // 队列项大小为 30 字节
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	// DDL_DelayMS(300);	
}



void config_production(void)
{
	// uint8_t config_product[] = {0x55,0xAA,0x01,0x11,0x00,0x0F,0x45,0x4a,0x34,0x49,0x36,0x6d,0x4e,0x48,0x6c,0x34,0x49,0x34,0x34,0x53,0x4b,0x54,0xFE};
	// uint8_t config_product[] = {0x55, 0xAA, 0x01, 0x11, 0x00, 0x0F, 0x45, 0x4a, 0x4a, 0x34, 0x49, 0x4b, 0x4b, 0x45, 0x45, 0x32, 0x6c, 0x34, 0x35, 0x35, 0x4b, 0x1D, 0xFE};

	//	国投
	uint8_t config_product[] = {0x55, 0xAA, 0x01, 0x11, 0x00, 0x0F, 0x45, 0x4b, 0x49, 0x35, 0x36, 0x36, 0x6d, 0x35, 0x36, 0x36, 0x33, 0x46, 0x6c, 0x34, 0x4b, 0xE6, 0xFE};
	// xQueueReset(uart_handle.uartqueue);
	uart_handle.send_data(config_product, sizeof(config_product));
	// uint8_t uartrecv[1] = "";
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
}

// void config_production_anyitong(void)
// {
// 	// uint8_t config_product[] = {0x55, 0xAA, 0x01, 0x11, 0x00, 0x0D, 0x45, 0x4a, 0x49, 0x45, 0x4b, 0x34, 0x4a, 0x49, 0x45, 0x34, 0x4b, 0x35, 0x36, 0x7C, 0xFE };
// 	// uint8_t config_product[] = {0x55,0xAA,0x01,0x11,0x00,0x0F,0x45,0x4a,0x34,0x49,0x36,0x6d,0x4e,0x48,0x6c,0x34,0x49,0x34,0x34,0x53,0x4b,0x54,0xFE};
// 	uint8_t config_product[] = {0x55, 0xAA, 0x01, 0x11, 0x00, 0x0F, 0x45, 0x4a, 0x4a, 0x34, 0x49, 0x4b, 0x4b, 0x45, 0x45, 0x32, 0x6c, 0x34, 0x35, 0x35, 0x4b, 0x1D, 0xFE};
// 	xQueueReset(uart_handle.uartqueue);
// 	uart_handle.send_data(config_product, sizeof(config_product));
// 	uint8_t uartrecv[100] = "";
// 	xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
// }

void clean_productionid(void)
{
	uint8_t config_product[] = {0x55, 0xAA, 0x01, 0x08, 0x00, 0x00, 0x08, 0xFE};
	// xQueueReset(uart_handle.uartqueue);
	uart_handle.send_data(config_product, sizeof(config_product));
	// uint8_t uartrecv[1] = "";
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
}


//正常第一版
void package_riskreportingdata(unsigned char *riskrebuff)
{
	// if (xSemaphoreTake(uart_mutex, portMAX_DELAY) == pdTRUE) {
	uint8_t uartrecv[1] = "";

	uint32_t i = 2;
	//传输大小床66/1024   67/160
	uint32_t bedType = 66;

	uint8_t riskreportingsend[1032];
	// uint8_t riskreportingsend[1];
	// riskreportingsend[0] = i;

	// memcpy(riskreportingsend, &i, sizeof(int)); 
	// memcpy(riskreportingsend + 4, &bedType, sizeof(int));
	// memcpy(riskreportingsend + 8, riskrebuff, 1024);
	
	memcpy(riskreportingsend, &i, sizeof(int)); 
	memcpy(riskreportingsend + 4, &bedType, sizeof(int));
	memcpy(riskreportingsend + 8, riskrebuff, 1024);

	// // 检查队列是否已满
    // if (uxQueueSpacesAvailable(protocol_handle.uartqueue) == 0) {
    //     // 队列已满，可添加错误处理
    //     free(riskreportingsend);
    //     return;
    // }

	xQueueReset(uart_handle.uartqueue);
	protocol_handle.package_message(riskreportingsend, sizeof(riskreportingsend));
	xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	// xSemaphoreGive(uart_mutex);
	// }
}

void send_SoScoordinateArr(void)
{
	uint8_t uartrecv[1] = "";
	uint32_t i = 3;
	uint8_t SendsosSignalOutArr[488];
	memcpy(SendsosSignalOutArr, &i, sizeof(uint32_t)); 
	
	memcpy(SendsosSignalOutArr + 4, nczallcode_Ytemp.sosSignalOut, sizeof(nczallcode_Ytemp.sosSignalOut)); 
	memcpy(SendsosSignalOutArr + 480, nczallcode_Ytemp.sosCoord, sizeof(nczallcode_Ytemp.sosCoord));
	protocol_handle.package_message(SendsosSignalOutArr, sizeof(SendsosSignalOutArr));
	xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
}

void get_4G_IMEI(void)
{
//55 AA 01 12 00 02 00 00 00 00 00 FE 
	uint8_t wifiuart[] = {0x55, 0xaa, 0x01, 0x12, 0x00, 0x02, 0x00, 0x00,0x00,0x00,0x00,0xfe};
	// xQueueReset(uart_handle.uartqueue);
	uart_handle.send_data(wifiuart, sizeof(wifiuart));
	// uint8_t uartrecv[100] = "";
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
	// xQueueReceive(uart_handle.uartqueue, uartrecv, RECVTIMEOUT);
}

PROTOCOL_HANDLE protocol_handle =
	{
		.package_message = package_message,
		// .get_net_time = get_net_time,
		// .package_realtimedata = package_realtimedata,
		.package_realsosdata = package_realsosdata,
		.get_net_status = get_net_status,
		.start_net_config = start_net_config,
		.package_oneminsdata = package_oneminsdata,
		.config_production = config_production,
		// .config_production_anyitong = config_production_anyitong,
		.clean_productionid = clean_productionid,
		.send_mcu_version = send_mcu_version,
		// .start_factory_test = start_factory_test,
		.package_riskreportingdata = package_riskreportingdata,
		// .get_wifi_mac = get_wifi_mac,
		.get_4G_IMEI = get_4G_IMEI,
		.package_A0message = package_A0message,
		.send_SoScoordinateArr = send_SoScoordinateArr,
};
